package com.zygh.questionnairesurvey.server;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.Problem;
import com.zygh.questionnairesurvey.entity.ProblemDetails;
import com.zygh.questionnairesurvey.entity.vo.UserInfoVO;
import com.zygh.questionnairesurvey.mapper.ProblemDetailsMapper;
import com.zygh.questionnairesurvey.mapper.ProblemMapper;
import com.zygh.questionnairesurvey.mapper.QuestionnaireMapper;
import com.zygh.questionnairesurvey.mapper.UserInfoMapper;
import javafx.concurrent.Worker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static cn.hutool.core.date.DateTime.now;

@Service
public class ProblemService extends ServiceImpl<ProblemMapper, Problem> {

    @Resource
    private ProblemMapper problemMapper;
    @Resource
    private ProblemDetailsMapper problemDetailsMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    public CommonResult listAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Problem> problemList = problemMapper.selectList(Wrappers.<Problem>lambdaQuery()
                .eq(Problem::getPid, 0)
                .orderByAsc(Problem::getSort));
        problemList.forEach(problemlist -> {
            List<Problem> problemList1 = problemMapper.selectList(Wrappers.<Problem>lambdaQuery()
                    .eq(Problem::getPid, problemlist.getId())
                    .orderByAsc(Problem::getSort));
            if (problemList1.size() > 0) {
                problemlist.setSecondLevelList(problemList1);
            }
            problemList1.forEach(list -> {
                List<Problem> problemList2 = problemMapper.selectList(Wrappers.<Problem>lambdaQuery()
                        .eq(Problem::getPid, list.getId())
                        .orderByAsc(Problem::getSort));
                if (problemList2.size() > 0) {
                    list.setSecondLevelList(problemList2);
                }
            });

        });

        if (problemList.size() > 0) {
            PageInfo<Problem> pageInfo = new PageInfo<>(problemList);
            return new CommonResult().success(pageInfo);
        }
        return new CommonResult().validateFailed("查询失败！");
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    public CommonResult getById(Integer id) {
        Problem userMenus = problemMapper.selectById(id);
        List<Problem> problemList = problemMapper.selectList(Wrappers.<Problem>lambdaQuery().eq(Problem::getPid, userMenus.getId()));

        if (userMenus != null && problemList.size() > 0) {
            userMenus.setSecondLevelList(problemList);
            problemList.forEach(problem -> {
                List<Problem> problemList2 = problemMapper.selectList(Wrappers.<Problem>lambdaQuery().eq(Problem::getPid, problem.getId()));
                problem.setSecondLevelList(problemList2);
            });
            return new CommonResult().success(userMenus);
        }
        return new CommonResult().validateFailed("查询失败！");
    }

    /**
     * 新增，插入所有字段
     *
     * @param problem 新增的记录
     * @return 返回影响行数
     */
    public CommonResult insert(Problem problem) {
        problem.setCreateTime(now());
        try {
            int insert = problemMapper.insert(problem);
            if (insert > 0) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            return new CommonResult().validateFailed("编号已存在");
        }
        return new CommonResult().validateFailed("新增失败！");
    }


    /**
     * 修改，修改所有字段
     *
     * @param problem 修改的记录
     * @return 返回影响行数
     */
    public CommonResult update(Problem problem) {
        try {
            int i = problemMapper.updateById(problem);

            if (i > 0) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            return new CommonResult().validateFailed("编号已存在");
        }
        return new CommonResult().validateFailed("修改失败！");
    }


    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    public CommonResult delete(Integer id) {
        int i = problemMapper.deleteById(id);
        if (i > 0) {
            List<Problem> problemList = problemMapper.selectList(Wrappers.<Problem>lambdaQuery().eq(Problem::getPid, id));
            if (problemList.size() > 0) {
                problemList.forEach(lists -> {
                    problemMapper.delete(Wrappers.<Problem>lambdaQuery().eq(Problem::getPid, lists.getId()));
                });
            }
            problemMapper.delete(Wrappers.<Problem>lambdaQuery().eq(Problem::getPid, id));
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败！");
    }

    /**
     * 根据警号查询该警员的调查问卷
     */
    public CommonResult findQuestionnaireByid(String username) {
        List<Problem> probelmById = problemMapper.findProbelmById(username, null, 1, null);
        probelmById.forEach(problem -> {
            List<Problem> probelmById1 = problemMapper.findProbelmById(username, null, 2, problem.getId());
            problem.setSecondLevelList(probelmById1);
            probelmById1.forEach(problem1 -> {
                List<Problem> probelmById2 = problemMapper.findProbelmById(username, null, 3, problem1.getId());
                problem1.setSecondLevelList(probelmById2);
                //插入二级个人问题详情描述
                List<ProblemDetails> problemDetails1 = problemDetailsMapper.selectList(Wrappers.<ProblemDetails>lambdaQuery()
                        .eq(ProblemDetails::getProblemId, problem1.getId())
                        .eq(ProblemDetails::getWarningsignal,problem1.getWarningsignal())
                        .orderByAsc(ProblemDetails::getCreateTime));
                problem1.setProblemDetails(problemDetails1);
                //插入三级个人问题详情描述
                probelmById2.forEach(problem2 -> {
                    List<ProblemDetails> problemDetails = problemDetailsMapper.selectList(Wrappers.<ProblemDetails>lambdaQuery()
                            .eq(ProblemDetails::getProblemId, problem2.getId())
                            .eq(ProblemDetails::getWarningsignal,problem2.getWarningsignal())
                            .orderByAsc(ProblemDetails::getCreateTime));
                    problem2.setProblemDetails(problemDetails);
                });
            });
        });
        return new CommonResult().success(probelmById);
    }

    /**
     * 根据警号和部门查询调查问卷信息
     *
     * @return CommonResult
     */
    public CommonResult findQuestionnaireByDeptid(String username, Integer deptid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UserInfoVO userInfo = userInfoMapper.findUserByName(username);
        List<Problem> probelmById = problemMapper.findProbelmById(username, deptid, 1, 0);
        probelmById.forEach(problem -> {
            List<Problem> probelmById2 = problemMapper.findProbelmById(username, deptid, 2, problem.getId());
            problem.setSecondLevelList(probelmById2);
            problem.setUserInfo(userInfo);
            probelmById2.forEach(problem1 -> {
                List<Problem> probelmById1 = problemMapper.findProbelmById(username, deptid, 3, problem1.getId());
                problem1.setSecondLevelList(probelmById1);
            });
        });

        PageInfo<Problem> pageInfo = new PageInfo<>(probelmById);
        return new CommonResult().success(pageInfo);
    }


}